-- Bishop
-- Created by cheny3 Sep/08/2016
-- 主教塞缪尔建筑


-- 内部函数声明
local createBishop;
local createBishopDisplay;

-- 初始化
function initBishop(self)
    self.item:removeAllChildren();

    self.item:setPositionY(10);

    if self.monsterNode.bishopDisplay then
        -- 先移除旧模型
        self.monsterNode:removeChild(self.monsterNode.bishopDisplay);
    end

    self.icon = nil;

    -- 创建其外观
    createBishop(self);
end

-- 创建主教
function createBishop(self)
    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            require "game/ui/form/dungeon/special/UIBishop"
            UIMgr.getCurrentScene():removeFormByName("UIBishop");
            local uiForm = UIBishop.create();
            UIMgr.getCurrentScene():addForm(uiForm);
        end
    end

    if self.monsterNode.bishopDisplay then
        -- 先移除旧模型
        self.monsterNode:removeChild(self.monsterNode.bishopDisplay);
    end

    local iconPath = getLevelItemIconPath("die_bishop");
    self.monsterNode.bishopIcon = ccui.ImageView:create(iconPath);
    self.monsterNode:addChild(self.monsterNode.bishopIcon);
    self.monsterNode.bishopIcon:setPosition(cc.p(-1,38));
    self.monsterNode.bishopIcon:setScale(1.05);
    setGrayMode(self.monsterNode.bishopIcon, true); -- 去色

    -- 显示底框
    self.monsterBgNode:setVisible(true);
    local monsterFrame = findChildByName(self.monsterBgNode, "monster_frame");
    monsterFrame:setTouchEnabled(true);
    monsterFrame:setVisible(true);
    monsterFrame:addTouchEventListener(onClicked);

    -- 显示血条和攻击
    self.barNode:setVisible(true);
    self.hpBarNode:setVisible(true);

    -- 活动副本部分楼层需要隐藏底座
    if ActivityDungeonM.needHideFrame() then
        monsterFrame:setOpacity(0);
    end

    -- 显示信息
    local function showBishopInfo()
        -- 创建模型
        local bishopDisplay = createBishopDisplay(self);
        self.monsterNode.bishopDisplay = bishopDisplay;
        self.monsterNode:addChild(bishopDisplay);
        self.monsterNode:setVisible(true);

        -- 重绘技能气泡
        self:redrawBishopSkill();

        -- 显示攻击和生命
        self.barNode:setVisible(true);
        self:redrawBishopAttackAndHp();

        -- 显示自动攻击轮次倒计时
        self:updateCountDown();
    end
    performWithDelay(self, showBishopInfo, 0.1);

    -- 注册技能气泡点击事件
    local dialogBg = findChildByName(self.bineSkillNode, "bg");
    dialogBg:setTouchEnabled(true);
    local function onDialogBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if ME.user.bishop.classId ~= PLANT_CLASS_BINE then
                return;
            end

            -- 判断是否死亡
            if ME.user.bishop:isDead() then
                return;
            end

            -- 判断魔法值
            local skillId = BishopM.getInitiativeSkill();
            if not BishopM.skillMpAllow(skillId) then
                alert(getLocStr("no_enough_mp"));
                return;
            end

            -- 音效
            AudioM.playFx("button_click");

            -- 打开施法界面
            require "game/ui/form/dungeon/special/UIBishopSelectTarget"
            UIMgr.getCurrentScene():removeFormByName("UIBishopSelectTarget");
            local uiForm = UIBishopSelectTarget.create(skillId);
            UIMgr.getCurrentScene():addForm(uiForm);
        end
    end
    dialogBg:addTouchEventListener(onDialogBgClick);
end

-- 创建模型
function createBishopDisplay(self)
    -- 创建模型（用召唤兽的）
    local bishop = ME.user.bishop;
    local classId = bishop.classId;
    local summonId = BishopM.query(classId, "summon_display")
    local bishopDisplay = SummonDisplay.create(summonId);

    -- 设置缩放比例
    local scale = SummonM.query(summonId, "model_scale");
    bishopDisplay:setScale(scale);

    -- 设置偏移量
    local offsetX = SummonM.query(summonId, "offset_x") or 0;
    local offsetY = SummonM.query(summonId, "offset_y") or 0;

    bishopDisplay:setPosition(tonumber(offsetX), tonumber(offsetY));
    return bishopDisplay;
end

-- 重绘攻击和生命
function redrawBishopAttackAndHp(self)
    local bishop = ME.user.bishop;
    if not bishop then
        -- 获取不到对象
        return;
    end

    -- 如果主教死亡了，需要隐藏血条和攻击条
    if bishop:isDead() then
        self.barNode:setVisible(false);
        self.monsterNode.bishopDisplay:setVisible(false);
        self.monsterNode.bishopIcon:setVisible(true);
    else
        self.barNode:setVisible(true);
        self.monsterNode.bishopDisplay:setVisible(true);
        self.monsterNode.bishopIcon:setVisible(false);
    end

    local hpLabel = self.hpLabel;
    local attackLabel = self.attackLabel;

    -- 显示血量和攻击
    local hp = bishop:getHp();
    hpLabel:setString(tostring(hp));

    local attack = bishop:getAttack();
    attackLabel:setString(tostring(attack));

    self:setBarStyle(hpLabel);
    self:setBarStyle(attackLabel);
end

-- 重绘技能气泡
function redrawBishopSkill(self)
    -- 如果学习了主动技能，则显示气泡
    local skillId = BishopM.getInitiativeSkill();
    local bishop = ME.user.bishop;
    if bishop:ownSkill(skillId) and not bishop:isDead() then
        -- 绘制图标
        local itemId = BishopM.getCostItem(skillId);
        local skillIconPath = getItemIconPath(ItemM.query(itemId, "icon"));
        local skillIconImg = findChildByName(self.bineSkillNode, "icon");
        skillIconImg:loadTexture(skillIconPath);

        -- 修正一下位置
        skillIconImg:setScale(0.7);
        skillIconImg:setPositionY(2);
        self.bineSkillNode:setPosition(cc.p(58, 72));

        if not BishopM.skillMpAllow(skillId) then
            -- MP限制
            setGrayTransMode(skillIconImg, true);
        else
            setGrayTransMode(skillIconImg, false);
        end

        -- 播放气泡动画
        if not self.bineSkillNode.isPlaying then
            playBubbleAnima(self.bineSkillNode, false);
            self.bineSkillNode.isPlaying = true;
        end
    else
        self.bineSkillNode:setVisible(false);
    end
end

-- 治疗特效
function bishopCureEffect(self, addList)
    -- 位置
    local pos = self.gridData:getPos();
    local grid = UIDungeonMgr:getCurLevel():getAllGrids()[pos];
    local bishopPos = cc.p(grid:getPositionX(), grid:getPositionY());
    local parentNode = UIDungeonMgr:getCurLevel():getEffectParent();

    -- 播放特效
    local i = 1;
    local bineCol = pos % DUNGEON_WIDTH;
    bineCol = iif(bineCol == 0, DUNGEON_WIDTH, bineCol);
    for _, arr in pairs(addList) do
        local attrib = arr[1];
        local value  = arr[2];
        if value > 0 then
            local xOffset = 0;
            local offset = cc.pAdd(bishopPos, cc.p(xOffset, 30));

            local function callFunc()
                if bineCol == 1 then
                    -- 格子在第一列，则特效还要往右偏移一点
                    offset = cc.pAdd(offset, cc.p(25, 0));
                elseif bineCol == DUNGEON_WIDTH then
                    -- 在最后一列，则特效还要往左偏移一点
                    local offsetX = -25;
                    if value >= 10 then
                        offsetX = -40;
                    elseif value >= 100 then
                        offsetX = -50;
                    end
                    offset = cc.pAdd(offset, cc.p(offsetX, 0));
                end

                playAttribUpEffect(parentNode, value, offset, attrib);
            end

            performWithDelay(self, callFunc, 0.01 + 0.8* (i - 1));
            i = i + 1;
        end
    end
end